/*
 * @lc app=leetcode.cn id=48 lang=javascript
 *
 * [48] 旋转图像
 */

// @lc code=start
/**
 * @param {number[][]} matrix
 * @return {void} Do not return anything, modify matrix in-place instead.
 */
function swap(matrix, x1, y1, x2, y2) {
  [matrix[x1][y1], matrix[x2][y2]] = [matrix[x2][y2], matrix[x1][y1]];
}

var rotate = function (matrix) {
  const n = matrix.length;
  // 副对角线翻转
  for (let i = 0; i < n; i++) {
    for (let j = 0; j < n - i - 1; j++) {
      swap(matrix, i, j, n - j - 1, n - i - 1);
    }
  }
  // 然后水平翻转
  for (let i = 0; i < Math.floor(n / 2); i++) {
    const temp = matrix[i];
    matrix[i] = matrix[n - i - 1];
    matrix[n - i - 1] = temp;
  }
};

// @lc code=end
